#include <stdio.h>



char * a;

int i=0;

int eval();

/**
 * File: eval.c
 * ----------------------------
 * 编译：
 * gcc -O0 -o eval eval.c
 * ----------------------------
 * 运行：
 * ./eval '* + 7 * * 4 6 + 8 9 5'
 * 
 */
int main(int argc, char ** argv){
    a = argv[1];
    i = 0;
    printf("%d\n", eval());
    return 0;
}

/**
 * Program 5.4 Recursive program to evaluate prefix expressions
 * To evaluate a prefix expression, we either convert a number from ASCII to binary (in the while loop at the end),
 * or perform the operation indicated by the first character in the expression on the two operands, evaluated recursively.
 * This function is recursive, but
 * it uses a global array containing the expression and an index to the current character in the expression.
 * The pointer is advanced past each subexpression evaluated.
 */
int eval(){
    int x = 0;
    while(a[i] == ' '){
        i++;
    }
    if (a[i] == '+') {
        i++;
        return eval() + eval();
    }
    if (a[i] == '*') {
        i++;
        return eval() * eval();
    }
    while ((a[i] >= '0') && (a[i] <= '9')) {
        x = 10*x + (a[i++] - '0');
    }
    return x;
}